﻿Imports System.Globalization

Public Class frmManageEmployeesDE
    Dim validator As PropertyValidator = New PropertyValidator
    Dim empMgr As New EmployeeManager


    Dim empInfoMgr As New EmployeeInfoManager

    Public Function ClearAllFields()
        validator.ResetControls(grpEmpDE)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function

    Private Sub btnClear_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        If Clipboard.GetData(DataFormats.Text) = "" Then
            validator.ResetControls(grpEmpDE)
        Else
            Me.Close()
        End If
    End Sub

    Private Function convertToDateTime(ByVal timeString As String, ByVal format As String) As DateTime
        Dim enUS As New CultureInfo("en-US")
        Dim TimeValue As DateTime

        If Date.TryParseExact(timeString, format, enUS, _
                             DateTimeStyles.None, TimeValue) Then
            Console.WriteLine("Converted '{0}' to {1} ({2}).", timeString, TimeValue, _
                              TimeValue.Kind)
        Else
            Console.WriteLine("'{0}' is not in an acceptable format.", DateString)

        End If
        Return TimeValue
    End Function

    Private Sub btnSave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If validator.IsEmptyString(txtUserName, "Username") Then Return

        If validator.IsEmptyString(txtPassword, "Password") Then Return
        If validator.IsEmptyString(txtConfirmPassword, "Password") Then Return
        If validator.IsEnteredInTheComboBoxList(comboRole, "Role") Then Return
        If validator.IsNumericString(txtSalary, "Salary") Then Return
        

        If Not txtPassword.Text.Equals(txtConfirmPassword.Text) Then
            MsgBox("Confirm password is not the same with Password")
        End If

        'Dim timeFormat As String = "HH:mm:ss"
        'Dim timeIn As DateTime = convertToDateTime(txtTimein.Text, timeFormat)
        'Dim timeOut As DateTime = convertToDateTime(txtTimeout.Text, timeFormat)


        Dim emp As New Employee
        emp.id = Clipboard.GetData(DataFormats.Text)
        emp.username = txtUserName.Text
        emp.password = txtPassword.Text
        emp.role = If((comboRole.SelectedIndex = 0), "1", "0")
        emp.salary = txtSalary.Text
        emp.time_in = dtpTimeIn.Value.ToString("HH:mm:ss")
        emp.time_out = dtpTimeOut.Value.ToString("HH:mm:ss")
        emp.flexitime = If((cbShiftType.Text.Equals("Flexitime")), "1", "0")

        Try
            If Clipboard.GetData(DataFormats.Text) = "" Then
                Dim newEmp As Employee = empMgr.insertEmployee(emp)

                Dim empInfo As New EmployeeInfo
                empInfo.employee_id = newEmp.id

                empInfoMgr.insertEmployeeInfo(empInfo)
                MsgBox("Successfully saved.")
            Else
                empMgr.updateEmployeeComplete(emp)
                MsgBox("Successfully updated.")
            End If
            ClearAllFields()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
        Me.Close()
    End Sub

    Private Sub frmManageEmployeesDE_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ClearAllFields()

        If Clipboard.GetData(DataFormats.Text) <> "" Then
            Try

                Dim empObj As Employee = empMgr.retrieveEmployeeById(Clipboard.GetData(DataFormats.Text))

                txtUserName.Text = empObj.username
                txtPassword.Text = If((empObj.password.Length > 0), "****", "")
                txtConfirmPassword.Text = If((empObj.password.Length > 0), "****", "")
                comboRole.SelectedIndex = If((empObj.role.Equals("1")), 0, 1)
                txtSalary.Text = empObj.salary
                dtpTimeIn.Text = empObj.time_in
                dtpTimeOut.Text = empObj.time_out
                cbShiftType.Text = If((empObj.flexitime.Equals("1")), "Flexitime", "Regular")
                btnSave.Text = "&Update"
                btnClear.Text = "&Cancel"
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If
    End Sub
End Class